package com.itcs.box.server.conf;

import com.itcs.box.BoxConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述:
 * <redis 发往通讯盒 消息订阅配置 验证通讯盒登录 消息订阅配置>
 *
 * @ClassName: RedisTopicBoxConfig
 * @Author: wanghui
 * @Date: 2019年05月17日 09:30:34
 *
 * @Copyright: 2019 Inc. All rights reserved. 注意: 本内容仅限于内部传阅,禁止外泄以及用于其他的商业目
 */
@Configuration
public class RedisTopicBoxConfig {

	@Autowired
	private TopicMsgReceiver topicMsgReceiver;

	// 初始化监听器
	@Bean
	public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
			MessageListenerAdapter listenerAdapter) {
		RedisMessageListenerContainer container = new RedisMessageListenerContainer();
		container.setConnectionFactory(connectionFactory);
		List<ChannelTopic> topics = new ArrayList<>();
		topics.add(new ChannelTopic(BoxConstants.PUBSUB_BOX_CHANNEL));
		topics.add(new ChannelTopic(BoxConstants.PUBSUB_BOXID_CHECK));
		container.addMessageListener(listenerAdapter(), topics);
		return container;
	}

	@Bean
	public MessageListenerAdapter listenerAdapter() {
		return new MessageListenerAdapter(topicMsgReceiver, "topicMsgReceiver");
	}

}
